package com.ljw.community.controller;

import com.ljw.community.entity.AjaxRespInfo;
import com.ljw.community.entity.Tag;
import com.ljw.community.entity.Topic;
import com.ljw.community.entity.TopicResp;
import com.ljw.community.service.TopicService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.List;

@Controller
@RequestMapping("/topicController")
public class TopicController extends BasicController {


    @Resource
    private TopicService topicService;

    @RequestMapping("/loadTopics")
    @ResponseBody
    public AjaxRespInfo<List<TopicResp>> loadTopics(String tag) {
        AjaxRespInfo<List<TopicResp>> ajaxRespInfo = new AjaxRespInfo<>();
        List<TopicResp> topicRespList = topicService.loadTopics(tag);
        if (topicRespList != null && topicRespList.size() > 0) {
            ajaxRespInfo.setData(topicRespList);
            ajaxRespInfo.setCode(0);
        } else {
            ajaxRespInfo.setCode(1);
            ajaxRespInfo.setMsg("该板块暂无话题");
        }
        return ajaxRespInfo;
    }

    @RequestMapping("/loadTags")
    @ResponseBody
    public AjaxRespInfo<List<Tag>> loadTags() {
        AjaxRespInfo<List<Tag>> ajaxRespInfo = new AjaxRespInfo<>();
        List<Tag> tagList = topicService.loadTags();

        if (tagList != null && tagList.size() > 0) {
            ajaxRespInfo.setData(tagList);
            ajaxRespInfo.setCode(0);
        } else {
            ajaxRespInfo.setCode(1);  //无任何板块
        }
        return ajaxRespInfo;
    }

    @RequestMapping("/loadMyTopics")
    @ResponseBody
    public AjaxRespInfo<List<TopicResp>> loadMyTopics(String date, String loginName) {
        AjaxRespInfo<List<TopicResp>> ajaxRespInfo = new AjaxRespInfo<>();
        List<TopicResp> topicRespList = topicService.loadMyTopics(date, loginName);
        if (topicRespList != null && topicRespList.size() > 0) {
            ajaxRespInfo.setData(topicRespList);
            ajaxRespInfo.setCode(0);
        } else {
            ajaxRespInfo.setCode(1);
            ajaxRespInfo.setMsg("未找到任何话题");
        }
        return ajaxRespInfo;
    }

    @RequestMapping("/deleteTopic")
    @ResponseBody
    public AjaxRespInfo deleteTopic(String id) {
        AjaxRespInfo ajaxRespInfo = new AjaxRespInfo();
        int rowNum = topicService.deleteTopic(id);
        if (rowNum == 1) {
            ajaxRespInfo.setMsg("删除成功");
            ajaxRespInfo.setCode(0);
        } else {
            ajaxRespInfo.setCode(1);
            ajaxRespInfo.setMsg("删除失败");
        }
        return ajaxRespInfo;
    }

    @RequestMapping("/changeState")
    @ResponseBody
    public AjaxRespInfo changeState(Topic topic) {
        AjaxRespInfo ajaxRespInfo = new AjaxRespInfo();
        int rowNum = topicService.updateTopic(topic);
        if (rowNum == 1) {
            ajaxRespInfo.setMsg("更改成功");
            ajaxRespInfo.setCode(0);
        } else {
            ajaxRespInfo.setCode(1);
            ajaxRespInfo.setMsg("删除失败");
        }
        return ajaxRespInfo;
    }

    @RequestMapping("/loadFollowedTopics")
    @ResponseBody
    public AjaxRespInfo<List<TopicResp>> loadFollowedTopics(String tag, String loginName) {
        AjaxRespInfo<List<TopicResp>> ajaxRespInfo = new AjaxRespInfo<>();
        List<TopicResp> topicRespList = topicService.loadFollowedTopics(tag, loginName);
        if (topicRespList != null && topicRespList.size() > 0) {
            ajaxRespInfo.setData(topicRespList);
            ajaxRespInfo.setCode(0);
        } else {
            ajaxRespInfo.setCode(1);
            ajaxRespInfo.setMsg("未找到任何话题");
        }
        return ajaxRespInfo;
    }

    @RequestMapping("/unfollowTopic")
    @ResponseBody
    public AjaxRespInfo unfollowTopic(String id) {
        AjaxRespInfo ajaxRespInfo = new AjaxRespInfo();
        int rowNum = topicService.unfollowTopic(id);
        if (rowNum == 1) {
            ajaxRespInfo.setMsg("取关成功");
            ajaxRespInfo.setCode(0);
        } else {
            ajaxRespInfo.setCode(1);
            ajaxRespInfo.setMsg("取关失败");
        }
        return ajaxRespInfo;
    }


    @RequestMapping("/followTopic")
    @ResponseBody
    public AjaxRespInfo followTopic(String topicId,String loginName) {
        AjaxRespInfo ajaxRespInfo = new AjaxRespInfo();
        int rowNum = topicService.followTopic(topicId,loginName);
        if (rowNum == 1) {
            ajaxRespInfo.setMsg("关注成功");
            ajaxRespInfo.setCode(0);
        } else {
            ajaxRespInfo.setCode(1);
            ajaxRespInfo.setMsg("请勿重复关注");
        }
        return ajaxRespInfo;
    }

    @RequestMapping("/addTopic")
    @ResponseBody
    public AjaxRespInfo addTopic(Topic topic, MultipartFile imgFile, String loginName) {
        AjaxRespInfo ajaxRespInfo = new AjaxRespInfo();
        String imgUrl = uploadFile("topicImg/", imgFile);
        topic.setImg(imgUrl);
        int rowNum = topicService.addTopic(topic, loginName);
        if (rowNum == 1) {
            ajaxRespInfo.setMsg("发布成功");
            ajaxRespInfo.setCode(0);
        } else {
            ajaxRespInfo.setCode(-1);
            ajaxRespInfo.setMsg("系统错误");
        }
        return ajaxRespInfo;
    }

    @RequestMapping("/modifyTopic")
    @ResponseBody
    public AjaxRespInfo modifyTopic(Topic topic, MultipartFile imgFile, String loginName) {
        AjaxRespInfo ajaxRespInfo = new AjaxRespInfo();
        String imgUrl = uploadFile("topicImg\\", imgFile);
        topic.setImg(imgUrl);
        int rowNum = topicService.modifyTopic(topic, loginName);
        if (rowNum == 1) {
            ajaxRespInfo.setMsg("修改成功");
            ajaxRespInfo.setCode(0);
        } else {
            ajaxRespInfo.setCode(-1);
            ajaxRespInfo.setMsg("系统错误");
        }
        return ajaxRespInfo;
    }

    @RequestMapping("/getTopicById")
    @ResponseBody
    public AjaxRespInfo<Topic> getTopicById(String id) {
        AjaxRespInfo<Topic> ajaxRespInfo = new AjaxRespInfo<>();
        Topic topic = topicService.getTopicById(id);
        if (topic != null) {
            ajaxRespInfo.setData(topic);
            ajaxRespInfo.setCode(0);
        } else {
            ajaxRespInfo.setCode(-1);
            ajaxRespInfo.setMsg("话题不存在");
        }
        return ajaxRespInfo;
    }

    @RequestMapping("/getDetail")
    @ResponseBody
    public AjaxRespInfo<TopicResp> getDetail(String id) {
        AjaxRespInfo<TopicResp> ajaxRespInfo = new AjaxRespInfo<>();
        if (id == null || "".equals(id)) {  //没有获得id直接返回
            ajaxRespInfo.setCode(1);
            ajaxRespInfo.setMsg("话题不存在");
            return ajaxRespInfo;
        }
        TopicResp topicResp = topicService.getTopicRespById(id);
        if (topicResp != null) {
            ajaxRespInfo.setData(topicResp);
            ajaxRespInfo.setCode(0);
        } else {
            ajaxRespInfo.setCode(1);
            ajaxRespInfo.setMsg("话题不存在");
        }
        return ajaxRespInfo;
    }

}
